<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:test="http://xsltut.googlecode.com"
	xmlns:dot="http://dotml.googlecode.com">
	<xsl:include href="common.xsl"/>
	<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
	<xsl:variable name="targetName" select="/sqlzen/@targetName"/>
	<test:testCase name="schema" inputSchema="${sqlzen-path}/schema/sqlzen.rng" outputSchema="${sqlzen-path}/external/schema/dotml.rng">
		<test:test name="empty">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test"/>
			</test:input>
			<test:expect>
				<dot:digraph name="Table diagram for test" rankdir="LR">
					<dot:nodeGlobalAttr fontname="Courier New" fontsize="10"/>
				</dot:digraph>
			</test:expect>
		</test:test>
		<test:test name="table">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test">
					<rule>
						<affixes>
							<dbObjects>
								<table suffix="T"/>
								<primaryKey prefix="IPK_"/>
								<foreignKey prefix="IFK_"/>
							</dbObjects>
							<entitySuffix name="Mst">
								<comment lang="ko" brief="마스터"
								>
								하위 엔티티가 존재하고 하위 엔티티에 대하여 일대다의 형식으로 기본 정보를 관리하는 엔터티 
							</comment>
							</entitySuffix>
							<attributeSuffix name="Seq">
								<comment lang="ko" brief="일련번호"
									>
								특정 Record의 고유 식별을 하기 위한 목적으로 특정 포맷 없이 단순 번호 형태로 부여되는 속성 
							</comment>
							</attributeSuffix>
						</affixes>
					</rule>
					<dictionary>
						<word name="Account" brief="Accnt">
							<comment lang="ko">유저 어카운트</comment>
						</word>
						<word name="Item" brief="Itm">
							<comment lang="ko">아이템</comment>
						</word>
						<tableName>
							<wordRef name="Account"/>
							<entitySuffixRef name="Mst"/>
							<comment lang="ko">어카운트 마스터</comment>
						</tableName>
						<tableName>
							<wordRef name="Item"/>
							<entitySuffixRef name="Mst"/>
							<comment lang="ko">아이템 마스터</comment>
						</tableName>
						<columnName>
							<wordRef name="Account"/>
							<attributeSuffixRef name="Seq"/>
							<comment lang="ko">어카운트 시퀀스</comment>
						</columnName>
						<columnName>
							<wordRef name="Item"/>
							<attributeSuffixRef name="Seq"/>
							<comment lang="ko">아이템 시퀀스</comment>
						</columnName>
					</dictionary>
					<database>
						<table name="AccountMstT">
							<comment lang="ko">어카운트마스터 테이블</comment>
							<columns>
								<column name="AccountSeq" type="integer" notNull="true" autoIncrement="true">
									<comment lang="ko" brief="어카운트PK">
										어카운트 테이블의 Primary Key
									</comment>
								</column>
								<column name="ItemSeq" type="integer" notNull="true">
									<comment lang="ko">
										아이템 외부참조
									</comment>
								</column>
							</columns>
							<constraints>
								<primaryKey column="AccountMstT.AccountSeq"/>
								<foreignKey column="AccountMstT.ItemSeq" referColumn="ItemMstT.ItemSeq"/>
							</constraints>
						</table>
						<table name="ItemMstT">
							<columns>
								<column name="ItemSeq" type="integer" notNull="true" autoIncrement="true">
									<comment lang="ko" brief="아이템PK">
										아이템테이블의 Primary Key
									</comment>
								</column>
							</columns>
							<constraints>
								<primaryKey column="ItemMstT.ItemSeq"/>
							</constraints>
						</table>
					</database>
				</sqlzen>
			</test:input>
			<test:expect>
				<dot:digraph name="Table diagram for test" rankdir="LR">
					<dot:nodeGlobalAttr fontname="Courier New" fontsize="10"/>
					<dot:subgraph id="cluster-AccountMstT" label="AccountMstT" bgcolor="lightblue" color="lightblue">
						<dot:node id="AccountMstT" shape="record">
							<dot:row cid="AccountSeq" label="*AccountSeq"/>
							<dot:row cid="ItemSeq" label="@ItemSeq"/>
						</dot:node>
					</dot:subgraph>
					<dot:subgraph id="cluster-ItemMstT" label="ItemMstT" bgcolor="lightblue" color="lightblue">
						<dot:node id="ItemMstT" shape="record">
							<dot:row cid="ItemSeq" label="*ItemSeq"/>
						</dot:node>
					</dot:subgraph>
					<dot:edge from="AccountMstT" fromCid="ItemSeq" to="ItemMstT" toCid="ItemSeq"/>
				</dot:digraph>
			</test:expect>
		</test:test>
	</test:testCase>
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="sqlzen">
		<dot:digraph rankdir="RL" ranksep="1">
			<xsl:attribute name="name">
				<xsl:value-of select="concat( 'Table diagram for ', @targetName )"/>
			</xsl:attribute>
			<dot:nodeGlobalAttr fontname="Courier New" fontsize="10"/>
			<dot:edgeGlobalAttr arrowsize="0.6"/>			
			<xsl:apply-templates/>
		</dot:digraph>
	</xsl:template>
	<xsl:template match="relationInfo"/>
	<xsl:template match="rule">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="database">
		<xsl:apply-templates/>
		<xsl:for-each select="table/constraints/foreignKey">
			<dot:edge>
				<xsl:attribute name="from">
					<xsl:value-of select="substring-before( @column, '.' )"/>
				</xsl:attribute>
				<xsl:attribute name="fromCid">
					<xsl:value-of select="substring-after( @column, '.' )"/>
				</xsl:attribute>
				<xsl:attribute name="to">
					<xsl:value-of select="substring-before( @referColumn, '.' )"/>
				</xsl:attribute>
				<xsl:attribute name="toCid">
					<xsl:value-of select="substring-after( @referColumn, '.' )"/>
				</xsl:attribute>
			</dot:edge>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="affixes"/>
	<xsl:template match="tags"/>
	<xsl:template match="dictionary"/>
	<xsl:template match="rule">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="forbiddenWords"/>
	<xsl:template match="table">
		<dot:subgraph bgcolor="lightblue" color="lightblue" fontname="Courier New" fontsize="11">
			<xsl:attribute name="id">
				<xsl:value-of select="concat( 'cluster-', @name )"/>
			</xsl:attribute>
			<xsl:attribute name="label">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<dot:node shape="record" height=".2">
				<xsl:attribute name="id">
					<xsl:value-of select="@name"/>
				</xsl:attribute>
				<xsl:apply-templates select="columns"/>
			</dot:node>
		</dot:subgraph>
	</xsl:template>
	<xsl:template name="putPrimaryKeyMark">
		<xsl:variable name="tn" select="../../@name"/>
		<xsl:variable name="cn" select="@name"/>
		<xsl:for-each select="../../constraints/primaryKey">
			<xsl:if test="substring-before( @column, '.' ) = $tn and substring-after( @column, '.' ) = $cn">*</xsl:if>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="putForeignKeyMark">
		<xsl:variable name="tn" select="../../@name"/>
		<xsl:variable name="cn" select="@name"/>
		<xsl:for-each select="../../constraints/foreignKey">
			<xsl:if test="substring-before( @column, '.' ) = $tn and substring-after( @column, '.' ) = $cn">@</xsl:if>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="columns">
		<xsl:for-each select="column">
			<dot:row>
				<xsl:attribute name="cid">
					<xsl:value-of select="@name"/>
				</xsl:attribute>
				<xsl:attribute name="label">
					<xsl:call-template name="putPrimaryKeyMark"/>
					<xsl:call-template name="putForeignKeyMark"/>
					<xsl:value-of select="@name"/>
				</xsl:attribute>
			</dot:row>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="comment"/>
	<xsl:template match="constraints">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="primaryKey"/>
	<xsl:template match="*">
		<xsl:text>&#10;</xsl:text>
		<xsl:text>Unmatched element found : '</xsl:text>
		<xsl:value-of select="name(..)"/>
		<xsl:text>/</xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:text>'</xsl:text>
	</xsl:template>
	<xsl:template match="text()"/>
</xsl:stylesheet>
